package com.mikhailenko.transportMVC.validator;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.mikhailenko.transportMVC.model.TableForm;
import com.mikhailenko.transportMVC.utils.Convertor;

@Component
public class FuzzyTableValidator implements Validator {

    
    
    @Override
    public boolean supports(Class<?> clazz) {
        return TableForm.class.isAssignableFrom(clazz);
    }


    
    @Override
    public void validate(Object target, Errors errors) {
        TableForm tableForm = (TableForm) target;
        

        for (int t = 0; t < tableForm.getTableList().size(); t++){
            List<List<String>> fuzzyTable = tableForm.getTableList().get(t).getTable();
                
            for (int i = 0; i < fuzzyTable.size(); i++) {
                for (int j = 0; j < fuzzyTable.get(i).size(); j++) {
                    if (fuzzyTable.get(i).get(j) == null || fuzzyTable.get(i).get(j).equals("")) {
                        errors.rejectValue("tableList["+t+"]", "empty cell[" + i + "][" + j + "]");
                    }
                }
            }

            if (errors.hasErrors()) {
                return;
            }

            for (int i = 0; i < fuzzyTable.size(); i++) {
                for (int j = 0; j < fuzzyTable.get(i).size(); j++) {
                    String[] triangleNumber = fuzzyTable.get(i).get(j).split(",");
                    if (triangleNumber.length < 3) {
                        errors.rejectValue("tableList["+t+"]", "Not full tringle numbet at cell[" + i + "][" + j + "]");
                    }
                }
            }

            if (errors.hasErrors()) {
                return;
            }

            for (int i = 0; i < fuzzyTable.size(); i++) {
                for (int j = 0; j < fuzzyTable.get(i).size(); j++) {

                    String[] triangleNumber = fuzzyTable.get(i).get(j).split(",");
                    double[] triangleNum = new double[triangleNumber.length];

                    for (int k = 0; k < triangleNumber.length; k++) {
                        try {
                            triangleNum[k] = Convertor.getDecimalFromFraction(String.valueOf(triangleNumber[k]));
                        } catch (NumberFormatException | NullPointerException e) {
                            errors.rejectValue("tableList["+t+"]", "Bad number format at cell[" + i + "][" + j + "]");
                        }

                    }
                    if (triangleNum[0] > triangleNum[1] || triangleNum[0] > triangleNum[2]
                            || triangleNum[1] > triangleNum[2]) {
                        errors.rejectValue("tableList["+t+"]", "Bad triangle number format at cell[" + i + "][" + j + "]");
                    }

                    if (i < j) {
                        if (triangleNum[0] == 0 || triangleNum[1] == 0 || triangleNum[2] == 0) {
                            errors.rejectValue("tableList["+t+"]", "Values in triangle "
                                    + "number can't be zero[" + i + "][" + j + "]");
                        }
                    }

                }
            }
            
        }
        
       
    }
}
